package com.cskaoyan.order.controller;

import com.cskaoyan.mall.commons.exception.ValidateException;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.order.dto.OrderDetailResponse;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.form.CreateOrderRequest;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import com.cskaoyan.order.constant.OrderRetCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 *
 * @since 2022/08/25 11:21
 * @author sunxuJ, ZDongMing
 */
@RestController
@RequestMapping("/shopping")
public class OrderController {

    @Autowired
    OrderCoreService orderCoreService;
    @Autowired
    OrderQueryService orderQueryService;


    @DeleteMapping("order/{id}")
    public ResponseData deleteOrder(@PathVariable("id") String orderId) {
        if (StringUtils.isEmpty(orderId.trim())) {
            throw new ValidateException(OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getCode(),
                    OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        }
        orderCoreService.deleteOrder(orderId);
        return new ResponseUtil().setData(null);
    }


    @PostMapping("cancelOrder")
    public ResponseData cancelOrder(@RequestBody Map map) {
        String id = (String) map.get("orderId");
        if (StringUtils.isEmpty(id)) {
            throw new ValidateException(OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getCode(),
                    OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        }
        orderCoreService.cancelOrder(id);
        return new ResponseUtil<>().setData(null);
    }


    // 创建订单
    @PostMapping("/order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest createOrderRequest, HttpServletRequest request){

        String userInfo = request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        createOrderRequest.setUserId(uid);
        createOrderRequest.setUniqueKey(UUID.randomUUID().toString());
        String orderId = orderCoreService.createOrder(createOrderRequest);
        return new ResponseUtil<>().setData(orderId,"success");
    }

    //查看当前用户所有订单
    @GetMapping("/order")
    public ResponseData queryAllOrders(OrderListRequest orderListRequest, HttpServletRequest request){
        String userInfo = request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());

        orderListRequest.setUserId(uid);
        OrderListResponse orderListResponse = orderQueryService.orderList(orderListRequest);
        return new ResponseUtil<>().setData(orderListResponse, "success");

    }

    //查看订单详情
    @GetMapping("/order/{id}")
    public ResponseData queryOrderDetail(@PathVariable("id") String id){

        OrderDetailResponse orderDetailResponse = orderQueryService.orderDetail(id);
        return new ResponseUtil<>().setData(orderDetailResponse,"success");
    }
}
